<?php
/**
 * 商品管理
 * @author hushuai
 *
 */
class ProductAction extends CommonAction{
	
	/**
	 * 定义model
	 */
	
	private $Product = "Product" ;//定义Product Model
	
	/**
	 * 菜品管理
	 */
	
	public function index(){
		if(IS_AJAX){
			$desc    = $this->_param("orderDirection","trim","desc");;
			$order   = $this->_param("orderName","trim","addtime");
			$pageNum = $this->_param("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_param("nowPage","intval",1);
			$restid  = $_GET['id'];
			$where   = $_POST['where'];
		   
			$map   = array("where"=>$where,"pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			$role  = session(C('USER_AUTH_ROLE'));
			
			if(C('ADMIN_ROLE')==session(C('USER_AUTH_ROLE'))){
				$map['noCheckRole'] = 1;
			}
			
			if($restid){
				$map['restid'] = $restid;
			}
			if(!$where['is_xiajia']){
				$map['is_xiajia'] = 2;
			}
			
			$json  = sendRest("RMS/Product/index",$map,"post");
			
			$data  = $json['data'];
			$count = $data['count'];
	
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'           => $row['id'],
						'rtitle'       => $row['rtitle'],
						'title'        => stripslashes($row['title']),
						'entitle'      => stripslashes($row['entitle']),
						'sprice'       => $row['sprice'],
						'mprice'       => $row['mprice'],
						'tprice'       => $row['tprice'],
						'dishes_num'   => $row['dishes_num'],
						'is_xiajia'    => $row['is_xiajia']=="2"?"否":"是",
						'addtime'      => date('Y-m-d',$row['addtime']),
					),
				);
				$jsonData['rows'][] = $entry;
			}
	
			$this->ajaxReturn($jsonData,"JSON");
		}else{
			$role  = session(C('USER_AUTH_ROLE'));
			if(C('ADMIN_ROLE')==session(C('USER_AUTH_ROLE'))){
				$map['noCheckRole'] = 1;
			}
			
			$list = sendRest("BDC/Outlet/view",$map);
			$this->assign("list", $list['data']);
			
			$id  = $this->_param("id","intval",0);
			$this->assign("id",$id);
			
			//根据餐厅id查询餐厅的菜品分类信息
			$restid = $this->_param('id');
			$node   = D('Product')->getSortByOutlet(array("restid"=>$restid));
			$this->assign('node',$node);
			
			//所有菜系
			$cuisine = sendRest("BDC/Outlet/dictCuisine");
			$this->assign("cuisine",$cuisine['data']['array']);
			 
			$this->display();
		}
	}
	/**
	 * 添加菜品
	 */
	public function add(){
		
		if(IS_AJAX){
			$json   = sendRest("RMS/Product/add", $_POST,"post");
			$this->ajaxReturn($json);
				
		}else{
			$map['id'] = $this->_param("id","intval",0);
			$jsons     = sendRest("BDC/Outlet/detail",$map);
			if(C('ADMIN_ROLE')==session(C('USER_AUTH_ROLE'))){
				$cat['noCheckRole'] = 1;
			} 
			$list      = sendRest("BDC/Outlet/view",$cat);
			
			//所有菜系
			$cuisine = sendRest("BDC/Outlet/dictCuisine");
			$this->assign("cuisine",$cuisine['data']['array']);
			
			$this->assign('UPLOAD_PATH',UPLOAD_PATH);
			$this->assign("detail", $jsons['data']);
			$this->assign("list",   $list['data']);
	
			$this->display();
		}
		 
		 
	}
	/**
	 * 修改菜品
	 */
	public function edit(){
		
		$product = D($this->Product);
		
		if(IS_AJAX){
			$json = sendRest("RMS/Product/edit",$_POST,"post");
			$this->ajaxReturn($json);
		}else{
	
			$id = $this->_get("id","intval",0);
			
			$dishesDetail = $product->dishesDetail(array("id"=>$id));
			$gnfr   = json_decode($dishesDetail['gnfr'],true);
			$this->assign("gnfr",$gnfr);
			
			$restid = $dishesDetail['restid'];
			if($restid){
				$this->assign("list",$product->getSub(array("restid"=>$restid)));
			}
			$this->assign("detail", $dishesDetail);
			$this->assign("single", $dishesDetail['single']);
			
			//所有菜系
			$cuisine = sendRest("BDC/Outlet/dictCuisine");
			$this->assign("cuisine",$cuisine['data']['array']);
			
			$this->assign('UPLOAD_PATH',UPLOAD_PATH);
			
			$this->display();
		}
	}
	/**
	 * 删除菜品
	 */
	public function del(){
		 
		if(IS_AJAX){
	
			$map['id'] = $this->_get("id","intval",0);
			$json = sendRest("RMS/Product/del",$map);
	
			$this->ajaxReturn($json);
		}
	}
	
	/**
	 * 批量复制属性
	 */
	public function fuzhi(){
		if(IS_AJAX){
			
			$data = sendRest("RMS/Product/fuzhi",$_POST);
			 
			$this->ajaxReturn($data);
		}else{
			$map['restid'] = $this->_param("restid","intval",0);
			$data = sendRest("RMS/Product/dview",$map);
			
			$this->assign("list",$data['data']);
			$this->assign("val",$this->_param("val"));
			$this->display();
		}
	}
	
	/**
	 * 批量删除菜品
	 */
	public function delall(){
			
		if(IS_AJAX){
	
			$map['val'] = $this->_param("val","trim","");
    		$data = sendRest("RMS/Product/delall",$map);
    	
			$this->ajaxReturn($data);
		}
	}
	/**
	 * 菜品批量上下架
	 */
	public function is_xsj(){
			
		if(IS_AJAX){
	
			$map['val'] = $this->_param("val", "trim",   "");
			$map['sxj'] = $this->_param("sxj", "intval", 0);
			
			$data = sendRest("RMS/Product/is_xsj",$map);
			$this->ajaxReturn($data);
		}
	}
	
	/**
	 * 库存商品批量显示隐藏
	 */
	public function isshow(){
		if(IS_AJAX){
		
			$map['val']    = $this->_param("val", "trim",   "");
			$map['isshow'] = $this->_param("isshow", "intval", 0);
				
			$data = sendRest("RMS/Product/isshow",$map);
			$this->ajaxReturn($data);
		}
	}
	
	/**
	 * 菜品分类管理
	 */
	public function sort(){
		
		$product = D($this->Product);
		
		$id     = $this->_param("id",     "intval",0);
		$restid = $this->_param("restid", "intval",0);
		
		if($restid){
			
			//根据餐厅id查询餐厅详情
			$outlet = $product->outletDetail(array("id"=>$restid));
			$this->assign("outlet",$outlet);
			$outletname = $outlet['title'].stripslashes($outlet['entitle']);
			$this->assign("outletname",$outletname);
			
			//根据餐厅id查询餐厅的菜品分类信息
			$node = $product->getSortByOutlet(array("restid"=>$restid));
			$this->assign("node",$node);
		}
		if($id){
			$detail = $product->getSortDetail(array("id"=>$id));
			$this->assign("detail",$detail);
		}
		if(IS_AJAX){
			//根据餐厅id查询餐厅的菜品分类信息
			$node = $product->getSortByOutlet(array("restid"=>$restid));
			$html = '<option value="">请选择菜品分类</option>';
			foreach($node as $k=>$v){
				$html .= '<option value="'.$v['id'].'">'.$v['title'].'</option>';
			}
			$this->ajaxReturn($html);
		}
		
		//餐厅列表
		$this->assign("rest", $product->outletView());
		$this->display();
	}
	
	/**
	 * 菜品分类添加页面，根据门店id异步查询菜品分类返回html
	 */
	public function sortByOutlet(){
		$product = D($this->Product);
		
		if(IS_AJAX){
			$restid = $this->_param("restid","intval",0);
			
			$this->assign("list",$product->sortView(array("restid"=>$restid)));
			$content = $this->fetch();
			$this->ajaxReturn($content);
		}
	}
	
	/**
	 * 菜品添加页面根据餐厅id查询餐厅菜品分类，返回html
	 */
	public function dishesByOutlet(){
		
		$product = D($this->Product);
		
		if(IS_AJAX){
			$restid = $this->_param("restid","intval",0);
			
			$this->assign("list",$product->getSub(array("restid"=>$restid)));
			$content = $this->fetch();
			$this->ajaxReturn($content);
		}
	}
	
	/**
	 * 添加菜品分类
	 */
	public function sortAdd(){
		
		$product = D($this->Product);
		if(IS_AJAX){
			$json = sendRest("RMS/Product/sortAdd",$_POST);
			$this->ajaxReturn($json);
		}else{
			$this->assign("list", $product->sortView());    //查询菜品分类列表
			$this->assign("rest", $product->outletView());  //查询门店列表
			$this->display();
		}
	}
	
	/**
	 * 修改菜品分类
	 */
	public function sortEdit(){
		 
		if(IS_AJAX){
			 
			$json = sendRest("RMS/Product/sortEdit",$_POST);
			$this->ajaxReturn($json);
			 
		}else{
			$map['id']  = $this->_param("id","intval",0);
			$json       = sendRest("RMS/Product/sortFind",$map);
	
			if($json['data']['pid']){
				$maps['id'] = $json['data']['pid'];
				$title      = sendRest("RMS/Product/sortFind",$maps);
				$this->assign("title",  $title['data']);
			}else{
				$this->assign("title", 0);
			}
	
			$this->assign("detail", $json['data']);
	
	
			$this->display();
		}
	}

	/**
	 * 删除菜品分类
	 */
	public function sortDel(){
		 
		if(IS_AJAX){
	
			$map['id'] = $this->_get("id","intval",0);
			$json = sendRest("RMS/Product/sortDel",$map);
	
			$this->ajaxReturn($json);
		}
	}
	
	/**
	 * 酒水管理
	 */
	public function drink(){
		 
		//饮品类型
		$drinkArr = sendRest("RMS/Product/drinkType");
		$this->assign("drinkArr",$drinkArr['data']['array']);
			
		//地址列表
		$dict_address = sendRest("BDC/Outlet/dictAddress");
		$this->assign("json",$dict_address['data']['zh-cn']['array']);
		
		if(IS_AJAX){
			 
			$desc    = $this->_param("orderDirection","trim","desc");;
			$order   = $this->_param("orderName","trim","addtime");
			$pageNum = $this->_param("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_param("nowPage","intval",1);
			$cityid  = $_GET['cityid'];
    		$where   = $_POST['where'];
	
			$map   = array("where"=>$where,"pageNum"=>$pageNum, "nowPage"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			
			if($cityid){
				$map['city_id'] = $cityid;
			}
			
			//echo createRestUrl('RMS/Product/drink',$map,'');exit;
			$json  = sendRest("RMS/Product/drink",$map);
			$data  = $json['data'];
			$count = $data['count'];
	
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
	
			foreach($data['list'] AS $row){
				
				$city     = $dict_address['data']['zh-cn']['array'][$row['city']][0];
				
				$entry = array('id'=>$row['id'],
					'cell'=>array(
							'id'      => $row['id'],
							'title'   => $row['title'].'<br/>'.stripslashes($row['entitle']),
							'city'    => $city,
							'num'     => $row['num'],
							'sort'    => $row['sort'],
							'isshow'  => $row['isshow']==1?"是":"否",
							'sprice'  => '￥'.$row['sprice'],
							'mprice'  => '￥'.$row['mprice'],
							'drink_num'=> $row['drink_num'],
							'type'    => $drinkArr['data']['array'][$row['type']][0],
							'addtime' => date('Y-m-d',$row['addtime']),
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
		}else{
			
			$cityid = $this->_param('cityid','intval',0);
			$this->assign('cityid',$cityid);
			
			$this->display();
		}
	}
	/**
	 * 添加酒水
	 */
	public function drinkAdd(){
		 
		if(IS_AJAX){
	
			$data = sendRest("RMS/Product/drinkAdd",$_POST,"post");
			$this->ajaxReturn($data);
	
		}else{

			//饮品类型
			$drinkArr = sendRest("RMS/Product/drinkType");
			$this->assign("drinkArr",$drinkArr['data']['array']);
	
			//地址列表
			$dict_address = sendRest("BDC/Outlet/dictAddress");
			$this->assign("json",$dict_address['data']['zh-cn']['array']);
			$this->assign('UPLOAD_PATH',UPLOAD_PATH);
			$this->display();
		}
	}
	/**
	 * 酒水修改
	 */
	public function drinkEdit(){
		 
		if(IS_AJAX){
		
			$data = sendRest("RMS/Product/drinkEdit",$_POST,"post");
			$this->ajaxReturn($data);
	
		}else{
			//饮品类型
			$drinkArr = sendRest("RMS/Product/drinkType");
			$this->assign("drinkArr",$drinkArr['data']['array']);

			//地址列表
			$dict_address = sendRest("BDC/Outlet/dictAddress");
			$this->assign("json",$dict_address['data']['zh-cn']['array']);
			
			$map['id'] = $this->_param("id","intval",0);
			$data = sendRest("RMS/Product/drinkDetail",$map);	
			$this->assign('UPLOAD_PATH',UPLOAD_PATH);
			$this->assign("detail",$data['data']);
			$this->assign("single", $data['data']['single']);
			$this->display();
		}
	}
	/**
	 * 删除酒水
	 */
	public function drinkDel(){

		$map['val'] = $this->_param("val","trim","");
		$data = sendRest("RMS/Product/drinkDel",$map);
		 
		$this->ajaxReturn($data);
	}
	
	/**
	 * 库存商品领用
	 */
	public function drinkGet(){
		
		$city = session(C('USER_AUTH_CITYID'));
		
		if(IS_AJAX){
			$_POST['city'] = $city;
			$json = sendRest('RMS/Product/drinkGet',$_POST);
			$this->ajaxReturn($json);
		}else{
			
			//快递员列表
			$map['cityid'] = $city;
			$map['role']   = 3;
			$data = M("Admin")->where($map)->select();
			$this->assign('user',$data);
			
			//库存商品
			$rows['city'] = $city;
			$json = sendRest("RMS/Product/drinkList",$rows);
			$this->assign('drink',$json['data']);
			
			$this->display();
		}
	}
	
	/**
	 * 库存商品领取列表
	 */
	public function drinkList(){
		
		//地址列表
		$dict_address = sendRest("BDC/Outlet/dictAddress");
		$this->assign("json",$dict_address['data']['zh-cn']['array']);
		
		if(IS_AJAX){
		
			$desc    = $this->_param("orderDirection","trim","desc");;
			$order   = $this->_param("orderName","trim","addtime");
			$pageNum = $this->_param("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_param("nowPage","intval",1);
			$where   = $_POST['where'];
			
			$where['startTime'] = strtotime($where['startTime']);
			$where['endTime']   = strtotime($where['endTime']);
			
			$map   = array("where"=>$where,"pageNum"=>$pageNum, "nowPage"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
				
			$json  = sendRest("RMS/Product/drinkGetList",$map);
			//echo createRestUrl("RMS/Product/drinkGetList",$map);exit;
			$data  = $json['data'];
			$count = $data['count'];
		
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
		
			foreach($data['list'] AS $row){
				
				$userid = $row['userid'];
				$user   = M('Admin')->find($userid);
				$city   = $dict_address['data']['zh-cn']['array'][$row['city']][0];
				
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'        => $row['id'],
						'title'     => $row['title'],
						'realname'  => $user['realname'],
						'city'      => $city,
						'num'       => $row['num'],
						'content'   => $row['content'],
						'drink_num' => $row['drink_num'],
						'addtime'   => date('Y-m-d',$row['addtime']),
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
		}elseif(IS_POST){
			$map['drinkid']   = $this->_param('drinkid','intval',0);
			$map['userid']    = $this->_param('userid','intval',0);
			$map['startTime'] = $this->_param('startTime','trim','');
			$map['endTime']   = $this->_param('endTime','trim','');
			
			$data = D('Product')->exportGetList($map);
		}else{
			
			$city = session(C('USER_AUTH_CITYID'));
			
			//快递员列表
			$map['cityid'] = $city;
			$map['role']   = 3;
			$data = M("Admin")->where($map)->select();
			$this->assign('user',$data);
			
			//库存商品
			$rows['city'] = $city;
			$json = sendRest("RMS/Product/drinkList",$rows);
			$this->assign('drink',$json['data']);
			
			$this->display();
		}
	}
	
	//批量导入
	public function import(){
		$restid = $this->_param("restid");
		
		$M_outlet = M('Outlet');
		$where = 'id='.$restid;
		$result = $M_outlet->where($where)->find();
		
		$this->assign("restid",$restid);
		$this->assign("restname",$result['title']);
		$this->assign("UPLOAD_PATH",UPLOAD_PATH);
		$this->display();
	}
	
	//执行读入
	public function read_excel(){
		$restid  = $this->_param("restid","trim","");
		$fileurl = $this->_param("fileurl");
		//菜品分类对应表
		$fieldname = array(
			0 => array('菜单种类','sorttitle'),
			1 => array('菜单种类英文','ensorttitle'),
			2 => array('菜单序号','dishes_num'),
			3 => array('中文名称','title'),
			4 => array('英文名称','entitle'),
			5 => array('价格','sprice'),
			6 => array('结算价格','mprice'),
			7 => array('中文说明','abstract'),
			8 => array('英文说明','enabstract')
		);	

		vendor('PHPExcel.PHPExcel');
		
		$objReader = PHPExcel_IOFactory::createReader(Excel5); 
		$objReader->setReadDataOnly(true); 
		$objPHPExcel   = $objReader->load($fileurl); 
		$objWorksheet  = $objPHPExcel->getActiveSheet(); 
		$highestRow    = $objWorksheet->getHighestRow(); 
		$highestColumn = $objWorksheet->getHighestColumn(); 
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); 
		$excelData = array(); 
		
		$addnum = 0;
		for ($row = 1; $row <= $highestRow; $row++) {
			$M_dishes = M('Dishes');
			$M_dishes->restid = $restid;
			$M_dishes->addtime = time();
			$M_dishes->solevar = create_id();
			$M_dishes->num = 100;
			$M_dishes->type = 1;
			for ($col = 0; $col < $highestColumnIndex; $col++) {
				$sortid = '';
				//处理分类
				if($row==1){
					break;
				}else{
					//处理菜品
					if($col==0){
						$title = (string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
						$entitle = (string)$objWorksheet->getCellByColumnAndRow($col+1, $row)->getValue();
						$M_dishsort = M('Dishsort');
						$where = 'restid='.$restid.' AND title="'.trim($title).'"';
						$result = $M_dishsort->where($where)->find();
						if($result['id']){
							$sortid = $result['id'];
						}else{
							$arr['restid']  = $restid;
							$arr['pid']     = 0;
							$arr['issub']   = 1;
							$arr['title']   = $title;
							$arr['entitle'] = $entitle;
							$arr['addtime'] = time();
							$arr['addid']   = session(C('USER_AUTH_KEY'));
							
							$sortid = $M_dishsort->add($arr);
						}
						$M_dishes->sortid = $sortid;
					}elseif($col==1){
						continue;
					}else{
						$value =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();		
						$M_dishes->$fieldname[$col][1] = $value;						
					}
				}
			}
			$M_dishes->add();
			$addnum++;
		}
		
		$ok['status'] = 1;
		$ok['addnum'] = $addnum;
		
		echo json_encode($ok);
	}
}

?>